﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using CH.Easy.Utility;
namespace CH.Wallpaper.Download
{
    public class PConlineDownloader : Downloader
    {

        public PConlineDownloader()
        {

        }
       
        public override List<string> ResolutionList
        {
            get
            {
                return new List<string> { "2560x1600", "1920x1200", "1680x1050", "1920x1440", "1600x1200", "1440x900", "1280x1024", "1024x768", "1280x800" };
            }
        }
        public override void Start()
        {
            //ThreadPool.QueueUserWorkItem(delegate
            //{

                ImageUrlList = new List<string>();
                string html = http.GetHtml(new HttpItem() { URL = this.URL, Encoding = "GBK" });
                if (!string.IsNullOrEmpty(html))
                {
                    Regex r = new Regex(@"imgSrc.{3}(?<src>[^,]*).,",RegexOptions.IgnoreCase);
                    MatchCollection mc = r.Matches(html);
                    foreach (Match m in mc)
                    {
                        string imgurl = m.Groups["src"].Value.Replace("100x75", this.Resolution).Replace("http://img", "http://imgrt");
                        imgurl = imgurl.Replace(imgurl.GetFileNameWidthExt(), "spcgroup/" + imgurl.GetFileNameWidthExt());
                        ImageUrlList.Add(imgurl);
                    }
                }

                if (!Directory.Exists(this.SavePath))
                {
                    Directory.CreateDirectory(this.SavePath);
                }


                WebClient client = new WebClient();
                foreach (var src in ImageUrlList)
                {

                    try
                    {

                        client.DownloadFile(new Uri(src), SavePath + "\\" + index + src.GetExt());
                        index++;
                    }
                    catch (Exception)
                    {


                    }
                }

                base.Loader_ItemDownloadCompleteEvent("");
            //});
        }

        public override bool IsAlbum(string url)
        {
            Regex r = new Regex(@"\/pic\/.*\.html");
            return r.IsMatch(url);
        }

        public override string FilterTitle(string title)
        {
            return title.RegexReplace("高清桌面壁纸壁纸图片大全_太平电脑网壁纸库", "").Replace("【", "").Replace("】", "");
        }
    }
}
